﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DictionaryScript : MonoBehaviour, ISerializationCallbackReceiver
{
    public bool modifyValues;
    [SerializeField]
    private DictionaryScriptable dictionaryScriptable;

    [SerializeField]
    private List<string> keys = new List<string>();

    [SerializeField]
    private List<Dialog> values = new List<Dialog>();

    public Dictionary<string, Dialog> myDictionary = new Dictionary<string, Dialog>();

    private void Awake()
    {
        for (int i = 0; i < Mathf.Min(dictionaryScriptable.Keys.Count, dictionaryScriptable.Values.Count); i++)
        {
            myDictionary.Add(dictionaryScriptable.Keys[i], dictionaryScriptable.Values[i]);
            


        }
    }

    public void DebugPrint()
    {
        int test = 0;
        foreach (var pair in myDictionary)
        {
            
            
            
        }
        test++;
    }

    public void OnBeforeSerialize()
    {
        
        if (!modifyValues)
        {


            keys.Clear();
            values.Clear();
            for (int i = 0;i<Mathf.Min(dictionaryScriptable.Keys.Count, dictionaryScriptable.Values.Count); i++)
            {
                keys.Add(dictionaryScriptable.Keys[i]);
                values.Add(dictionaryScriptable.Values[i]);

                
            }
        }
    }

    public void OnAfterDeserialize()
    {
        
    }
    public void Deserealize()
    {
        
        //myDictionary = new Dictionary<string,Dialog>();
        
        dictionaryScriptable.Keys.Clear();
        dictionaryScriptable.Values.Clear();
        myDictionary.Clear();
        for (int i = 0; i < Mathf.Min(keys.Count, values.Count); i++)
        {
            dictionaryScriptable.Keys.Add(keys[i]);
            dictionaryScriptable.Values.Add(values[i]);
            myDictionary.Add(keys[i], values[i]);
            
        }
        modifyValues = false;
    }

}
